package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.yunhe.dao.BoardDao;
import cn.yunhe.dao.UserDao;
import cn.yunhe.dao.impl.BoardDaoImpl;
import cn.yunhe.dao.impl.UserDaoImpl;
import cn.yunhe.entity.Board;
import cn.yunhe.entity.User;

/**
 * Servlet implementation class UserLogin
 */
@WebServlet("/UserLogin")
public class UserLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserLogin() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//测试servlet
		//System.out.println("hey");
		UserDao ud= new UserDaoImpl();
		String name=request.getParameter("uname");
		String pwd=request.getParameter("pwd");
		User user=ud.login(name, pwd);
		if(user!=null){
			request.getSession().setAttribute("CURR_USER", user);
			BoardDao boardDao = new BoardDaoImpl();	     
	        List<Board> blist = boardDao.findAllBoard();	           
	        request.setAttribute("blist", blist);
			request.getRequestDispatcher("home.jsp").forward(request, response);
		}else{
			response.sendRedirect("login.jsp");
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
